setwd("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/GB")
library(haven)
library(dplyr)
library(janitor)

Jul2020 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/GB/Jul2020/UCL_DEL_Sandbox_GB_Wave1_client.sav")
Jun2021 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/GB/Jun2021/UCL_DEL_Sandbox_June2021_GB_client.sav")
Jun2022 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/GB/Jun2022/UCL_DEL_Sandbox_GB_June2022_client.sav")
#Mar2023 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/GB/Mar2023/UCL_DEL_Sandbox_GB_Wave1_client.sav")
Oct2023 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/GB/Oct2023/UCL_DEL_Sandbox_GB_Oct2023_client.sav")

names(Jul2020)
names(Jun2021)
names(Jun2022)

#Jul2020:
  #g1wash_ and g1mat_ and g1pov_ to g12 variables now coded 1-11 and 12 idk, recode to 0-10 and 11 --> is this conjoint part? do i combine the answers?

#rename weighting variables to Weight for Jul2020 and Jun2021
Jul2020 <- Jul2020 %>% 
  rename(Weight = WeightSandboxW1)
Jun2021 <- Jun2021 %>% 
  rename(Weight = WeightSandboxGB)

#remove all the _w6 from Jun2022
Jun2022 <- Jun2022 %>%
  rename_with(~paste0(sub("_w6*", "", .)), -1)

GB_comp <- compare_df_cols(Jul2020, Jun2021, Jun2022)

###Jul2020

#change intcon, conshould, conspirx and g1-g12 etc from 1-12 to 0-11
Jul2020 <- Jul2020 %>%
  mutate(intcon = intcon-1,
         conshould = conshould-1)

Jul2020 <- Jul2020 %>%
  mutate(across(conspirx_1:conspirx_5, ~  . -1),
         across(g1wash_p:g12wash_fn, ~  . -1))

table(Jul2020$intcon)
table(Jul2020$conshould)
table(Jul2020$conspirx_1)
table(Jul2020$conspirx_2)
table(Jul2020$conspirx_3)
table(Jul2020$conspirx_4)
table(Jul2020$conspirx_5)
table(Jul2020$g12wash_fn)


###Jun 2022
table(Jun2022$list_control)
table(Jun2022$treat_control)
Jun2022 <- Jun2022 %>%
  mutate(list_control = list_control-1,
         treat_control = treat_control-1)

write_sav(Jul2020, "UCL_DEL_Sandbox_GB_Wave1_client_new.sav")
write_sav(Jun2021, "UCL_DEL_Sandbox_GB_June2021_client_new.sav")
write_sav(Jun2022, "UCL_DEL_Sandbox_GB_June2022_client_new.sav")

#checking

Jul2020a <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/GB/Jul2020/UCL_DEL_Sandbox_GB_Wave1_client_new.sav")
Jun2021a <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/GB/Jun2021/UCL_DEL_Sandbox_GB_June2021_client_new.sav")
Jun2022a <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/GB/Jun2022/UCL_DEL_Sandbox_GB_June2022_client_new.sav")

names(Jul2020a)
names(Jun2021a)
names(Jun2022a)

table(Jul2020a$intcon)
table(Jul2020a$conshould)
table(Jul2020a$conspirx_1)
table(Jul2020a$conspirx_2)
table(Jul2020a$conspirx_3)
table(Jul2020a$conspirx_4)
table(Jul2020a$conspirx_5)
table(Jul2020a$g12wash_fn)

table(Jun2022a$list_control)
table(Jun2022a$treat_control)

GB_comp <- compare_df_cols(Jul2020a, Jun2021a, Jun2022a, Oct2023)

library(sjlabelled)
Oct2023$connect <- add_labels(Oct2023$connect, labels = c(`10 - A very strong connection` = 11))
Oct2023$fatigue <- add_labels(Oct2023$fatigue, labels = c(`0 - Not at all tired of seeing images like this one` = 1,
                                                          `10 - Very tired of seeing images like this one` = 11))
Oct2023$motivated <- add_labels(Oct2023$motivated, labels = c(`0 - Not at all motivated` = 1,
                                                          `10 - Very motivated` = 11))


Oct2023 <- Oct2023 %>%
  rename(religion = profile_religion,
         Weight = weight) %>%
  mutate(eff_follow_dy_a = eff_follow_dy_a-1,
         eff_follow_dn_a = eff_follow_dn_a-1,
         eff_follow_dy_b = eff_follow_dy_b-1,
         eff_follow_dn_b = eff_follow_dn_b-1,
         eff_follow_dy_c = eff_follow_dy_c-1,
         eff_follow_dn_c = eff_follow_dn_c-1,
         eff_follow_dy_d = eff_follow_dy_d-1,
         eff_follow_dn_d = eff_follow_dn_d-1,
         eff_follow_ey_e = eff_follow_ey_e-1,
         eff_follow_en_e = eff_follow_en_e-1,
         connect = connect-1,
         hopeful = hopeful-1,
         fatigue = fatigue-1,
         motivated = motivated-1,
         sustain = sustain-1)

write_sav(Oct2023, "UCL_DEL_Sandbox_GB_Oct2023_Data.sav")

#checking if files are the SAME
Feb2020 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/GB/wave1 feb2020/UCL_DEL_Sandbox_GB_Wave1_client.sav")
Jul2020a <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/GB/Jul2020/UCL_DEL_Sandbox_GB_Wave1_client_new.sav")

GB_comp <- compare_df_cols(Feb2020, Jul2020a)
table(Jul2020a$intcon)
table(Jul2020a$conshould)
table(Jul2020a$conspirx_1)
table(Feb2020$intcon)
table(Feb2020$conshould)
table(Feb2020$conspirx_1)
